package cc.jq1024.middleware.test;

import cc.jq1024.middleware.token.service.ITokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @author li--jiaqiang
 * @date 2024−09−10
 */
@RestController
public class UserController {

    @Resource
    private ITokenService tokenService;

    private final Logger logger = LoggerFactory.getLogger(UserController.class);

    /**
     * <a href="http://localhost:8082/api/queryUserInfo?userId=111">...</a>
     */
    @RequestMapping(path = "/api/queryUserInfo", method = RequestMethod.GET)
    public UserInfo queryUserInfo(@RequestParam("userId") String userId) {
        UserInfo userInfo = new UserInfo("虫虫:" + userId, 19, "天津市东丽区万科赏溪苑14-0000");

        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        String jwt = tokenService.createJWT(map);
        logger.info("token: {}", jwt);
        return userInfo;
    }

    /**
     * <a href="http://localhost:8082/api/getUserId">...</a>
     */
    @RequestMapping(path = "/api/getUserId", method = RequestMethod.GET)
    public String getUserId() {
        String token = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJjY2ZAbGlqaWFxaWFuZyIsImV4cCI6MTcyNTk5MDM0OSwidXNlcklkIjoiMTExIiwiaWF0IjoxNzI1OTQ3MTQ5LCJqdGkiOiI5NTAxODZlYi0wNTE2LTQxMzAtYTI0OC0zN2NlNDhkYWUzZTUifQ.OAeuzcYJR-z45MWSYCWV1iGkv9oeI2ieRsp1qDtMVUA";
        return tokenService.propertyValue(token, "userId");
    }
}